package com.hxzy.commons.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hxzy.commons.model.CmsPower;
import com.hxzy.commons.service.IAuthService;
import com.hxzy.core.page.entity.PageParams;
import com.hxzy.core.page.entity.Pagination;

/**
 * 角色管理
 * @author Thinkpad
 *
 */

@Controller
@RequestMapping("/auth/power")
public class PowerManageAction {
	@Autowired
	private IAuthService authService;
	@RequestMapping(value="/loadPowerList.do") 
	public ModelAndView loadPowerList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String powerName = request.getParameter("powerName");
		Pagination page = authService.loadPowerList(pageParams,powerName);
		
		model.addAttribute("page",page); 
		model.addAttribute("powerName",powerName);  
		return new ModelAndView("/app/module/power/powerList",model); 
	}
	/**
	 * 新增一个权限
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOnePower.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOnePower(CmsPower powerobj,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("rolecode", powerobj);
		Object existData = authService.getUniqueObject(CmsPower.class, query);
		if(null != existData){
			return "该角色已存在！";
		}
		powerobj.setIsuse(1l);
		authService.save(powerobj); 
		 
		return "保存成功！";
	}
	/**
	 * 修改一个权限
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOnePower.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneUser(CmsPower powerobj,HttpServletRequest request,ModelMap model){ 
		//authService.save(userobj);
		return "修改成功！";
	}
	
	
	/**
	 * 删除权限
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOnePower.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOnePower(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		CmsPower powerobj = (CmsPower)authService.findByID(CmsPower.class, new Long(id));
		powerobj.setIsuse(0l);
		authService.saveObject(powerobj);
		return "删除成功！";
	}
}
